{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "逻辑回归通常使用准确率、精确率、召回率、F1分数等分类指标。\n",
    "线性回归使用均方误差（MSE）、平均绝对误差（MAE）、R²等回归指标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型准确率: 0.87\n",
      "预测值前5个:  [0 1 1 0 0]\n",
      "实际值前5个:  [0 1 1 1 0]\n"
     ]
    }
   ],
   "source": [
    "# 导入必要的库\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.datasets import make_classification\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# 生成一个简单的二分类数据集\n",
    "X, y = make_classification(n_samples=1000, n_features=2, \n",
    "                           n_informative=2, n_redundant=0, \n",
    "                           random_state=42)\n",
    "\n",
    "# 将数据分为训练集和测试集，测试集占30%\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
    "\n",
    "# 创建逻辑回归模型\n",
    "model = LogisticRegression()\n",
    "\n",
    "# 训练模型\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "# 在测试集上进行预测\n",
    "y_pred = model.predict(X_test)\n",
    "\n",
    "# 评估模型的准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f'模型准确率: {accuracy:.2f}')\n",
    "\n",
    "# 查看模型预测的前5个结果\n",
    "print(\"预测值前5个: \", y_pred[:5])\n",
    "print(\"实际值前5个: \", y_test[:5])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通用代码示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型准确率: 1.00\n",
      "混淆矩阵:\n",
      "[[19  0  0]\n",
      " [ 0 13  0]\n",
      " [ 0  0 13]]\n",
      "分类报告:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00        19\n",
      "           1       1.00      1.00      1.00        13\n",
      "           2       1.00      1.00      1.00        13\n",
      "\n",
      "    accuracy                           1.00        45\n",
      "   macro avg       1.00      1.00      1.00        45\n",
      "weighted avg       1.00      1.00      1.00        45\n",
      "\n",
      "预测值前5个:  [1 0 2 1 1]\n",
      "实际值前5个:  [1 0 2 1 1]\n"
     ]
    }
   ],
   "source": [
    "# 导入必要的库\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score, confusion_matrix, classification_report\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.datasets import load_iris  # 可替换为其他数据集\n",
    "\n",
    "# 1. 数据准备：使用鸢尾花数据集（多分类问题）\n",
    "data = load_iris()  # 这里是多分类数据集\n",
    "X = data.data  # 特征\n",
    "y = data.target  # 标签\n",
    "\n",
    "# 2. 数据标准化（可选，但在逻辑回归中通常推荐）\n",
    "scaler = StandardScaler()\n",
    "X = scaler.fit_transform(X)\n",
    "\n",
    "# 3. 分割训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
    "\n",
    "# 4. 创建逻辑回归模型（可根据需要调整参数）\n",
    "model = LogisticRegression(multi_class='auto', solver='lbfgs', max_iter=1000)\n",
    "\n",
    "# 5. 训练模型\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "# 6. 在测试集上进行预测\n",
    "y_pred = model.predict(X_test)\n",
    "\n",
    "# 7. 评估模型性能\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "conf_matrix = confusion_matrix(y_test, y_pred)\n",
    "class_report = classification_report(y_test, y_pred)\n",
    "\n",
    "print(f'模型准确率: {accuracy:.2f}')\n",
    "print('混淆矩阵:')\n",
    "print(conf_matrix)\n",
    "print('分类报告:')\n",
    "print(class_report)\n",
    "\n",
    "# 8. 输出预测结果\n",
    "print(\"预测值前5个: \", y_pred[:5])\n",
    "print(\"实际值前5个: \", y_test[:5])\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
